package com.GPT.service.impl;

import com.GPT.common.WeixinUserInfo;
import com.GPT.service.iface.IWeixinService;
import com.GPT.service.iface.WeixinUserInfoRepository;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;

@SuppressWarnings("all")
@Service("IWeixinService")
public class WeixinService implements IWeixinService {
    private Logger logger = LogManager.getLogger(getClass());

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private WeixinUserInfoRepository userInfoRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Value("${weixin.open.connect}")
    private String weixin_open_connect;//网站跳转

    @Value("${weixin.open.sns.oauth2}")
    private String weixin_open_sns_oauth2;//获取access token

    @Value("${weixin.open.sns.userinfo}")
    private String weixin_open_sns_userinfo;//根据access token 获取用户信息

    /**
     * 获取access_token
     *
     * @param code
     * @return
     */
    private JSONObject getAccessToken(String code) {
        String tokenJson = null;
        String url = String.format(weixin_open_sns_oauth2, code);
        String resultStr = restTemplate.getForObject(url, String.class);
        JSONObject result = JSONObject.parseObject(resultStr);
        if (result.getString("access_token") != null) {
            return result;
        }
        logger.error(String.format("获取accesstoken错误  %s", result.toJSONString()));
        return result;
    }

    /**
     * 获取用户信息
     *
     * @param code
     * @return
     */
    @Override
    public JSONObject getUserInfo(String code) {
        try {
            JSONObject accessToken = getAccessToken(code);
            if (accessToken.getString("access_token") == null) {
                return accessToken;
            }
            String token = accessToken.getString("access_token");
            String openid = accessToken.getString("openid");
            String url = String.format(weixin_open_sns_userinfo, token, openid);
            String resultStr = restTemplate.getForObject(url, String.class);
            resultStr = new String(resultStr.getBytes("ISO-8859-1"), "UTF-8");
            JSONObject result = JSONObject.parseObject(resultStr);

            if (result.getString("openid") != null) {
                //saveUserInfo(result); // 保存用户信息到数据库
                return result;
            }

            logger.error(String.format("获取用户信息错误  %s", result.toJSONString()));
            return result;
        } catch (UnsupportedEncodingException e) {
            logger.error(String.format("转码错误  %s", e.getMessage()));
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 保存用户信息到数据库
     *
     * @param userInfo 用户信息JSON对象
     */
    @Override
    public void saveUserInfo(JSONObject userInfo) {
        WeixinUserInfo user = new WeixinUserInfo();
        user.setOpenid(userInfo.getString("openid")); // 微信号
        user.setNickname(userInfo.getString("nickname")); // 微信名
        user.setHeadimgurl(userInfo.getString("headimgurl")); // 头像 URL
        //user.setCreateTime(new Timestamp(System.currentTimeMillis())); // 创建时间

        try {
            userInfoRepository.save(user);
        } catch (Exception e) {
            logger.error("保存用户信息到数据库时发生错误", e);
            throw new RuntimeException("保存用户信息失败", e);
        }
    }
}